#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include "msgq.h"
#include "tools.h"


Ctos ctos;
Stoc stoc;

void destory_acc(void)
{
	char path[PATH_MAX];
	sprintf(path,"%s/%u",ACC_PATH,ctos.acc.id);
	pf("acc path:%s\n",path);
	int fd = open(path,O_RDONLY|O_EXCL,0644);
	if(ERROR == fd)
	{
		perror("open acc");
		strcpy(stoc.str,"您输入的帐号不存在！\n");
	}
	else
	{
		Account aco;
		read(fd,&aco,sizeof(aco));
		if(ctos.acc.pass != aco.pass)
		{
			strcpy(stoc.str,"密码错误！销户失败！\n");
		}
		else
		{
			close(fd);
			unlink(path);
			strcpy(stoc.str,"销户成功！\n");
		}
	}
	
	stoc_send(&stoc);//返回消息
}

int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_DESTORY);
		stoc.type = ctos.pid;//反馈消息类型为进程号
		destory_acc();
	}
}
